from regular_matching_rules import get_keywordTredTree_instance
from spidertools.utils.xpath_utils import get_alltext
from spidertools.utils.text_utils import replace_punctuation
from info_fsm_utils import clean_triedtree_keys
import re

def clean_table_text(text):
    text = replace_punctuation(text)
    text = text.replace("&nbsp;", "")
    text = text.replace("&nbsp", "")
    text = text.replace('\n', "")
    text = text.replace("\t", "")
    text = text.replace(' ', "")
    text = text.replace(' ', "")
    text = text.replace("\u3000", "")
    text = text.replace("\r", "")
    text = text.strip()
    return text


def clean_triedtree_keys(text):
    triedTree = get_keywordTredTree_instance()
    node_text = replace_punctuation(text.strip())
    #clean_text = replace_punctuation(node_text.replace(" ", "").replace(" ", ""))  # nbsp就是那些没用的字符
    clean_text  = replace_punctuation(re.sub(r"[\s ]*(\([^\(]*?\)){0,1}[\s ]*", "", node_text))

    keywords, types, _ = triedTree.process(clean_text)
    if len(keywords) == 1:
        search_keyword = ["^([\d一二三四五六七八九十][、.]){0,1}"]
        for k in keywords[0]:
            search_keyword.append(k)
            search_keyword.append("[\s ]*(\([^\(]*?\)){0,1}[\s ]*")  # nbsp不换行空格
        search_keyword = "".join(search_keyword) + "(:{0,1})$"
        replace_keyword = clean_text
        replace_keyword = replace_keyword.replace("\\","\\\\")

        node_text = re.sub(search_keyword, replace_keyword, node_text)

    return node_text

def check_is_tablekey(key,origin_text):
    '''
    判断是不是一个合法的key
    目前出现的情况：
     项目工程
     项目工程:
     1.项目名称：
     1.项目名称
     十、项目名称
     十、项目名称：
    '''
    type = ''
    key_regex = '^([\d一二三四五六七八九十][、.]){0,1}%s:{0,1}$' % key
    match = re.search(key_regex, origin_text)
    if match:
        match_result = match[0]
        if origin_text == match_result:
            type = "table_key"
    return type


def get_tr_parsed_value( tr_node,triedTree,text_replace_fun=None):
    '''
    将tr下面的td里面的内容转换为key,value形式
    :param tr_node:
    :return:
    '''
    td_nodes = tr_node.xpath("./*")
    td_nodes = [node for node in td_nodes if node.root.tag == 'td' or node.root.tag == 'th']
    td_node_texts = [get_alltext(td_node) for td_node in td_nodes]
    td_node_texts = [clean_table_text(text) for text in td_node_texts]
    td_node_texts = [clean_triedtree_keys(text) for text in td_node_texts]
    if text_replace_fun:
        td_node_texts = [text_replace_fun(text) for text in td_node_texts]

    td_types = []
    td_values = []

    for text in td_node_texts:
        type = ""
        value = ""
        if text:
            keywords, types, end_positions = triedTree.process(text)
            value = text
            if len(keywords) == 1:
                use_types = [v for v in types[0] if v != 'ignore' and not v.startswith('parsemode')]
                if use_types :
                    type = check_is_tablekey(keywords[0],value)
                    if type == 'table_key' :
                        value = text.replace(":"," ")
        td_types.append(type)
        td_values.append(value)

    return td_types, td_values

def get_first_trs(table_node,pattern = './*',deep_len = 0):
    '''
    获取到table
    '''
    nodes = table_node.xpath(pattern)
    found_tr = False
    for node in nodes:
        if node.root.tag == 'tr':
            found_tr = True
            break

    if found_tr:
        pattern = pattern[:-2] + '/tr'
        nodes = table_node.xpath(pattern)
    else:
        # 防止无限嵌套
        if deep_len < 3:
            deep_len += 1
            nodes =  get_first_trs(table_node,pattern+'/*',deep_len)
    return nodes